#Create an ICNET router build package.

echo "ARE YOU SURE THAT YOU HAVE AN UP TO DATE _RELEASE_ BUILD OF THE ROUTER?"
Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

$buildOutputDir=pwd;

# Step 1: Create a new directory for the install package
$dateString = (get-date).ToString();
$dateString = $dateString.Replace("/","-");
$dateString = $dateString.Replace(":",".");

$outputLocation = "C:\ICNET-Build\Build-Zip\";
if (-not(test-path -path $outputLocation))
{
	mkdir $outputLocation
}

$outputName = "ICNET Router Clean Installer "+$dateString
mkdir $outputLocation$outputName | out-null;
mkdir $outputLocation$outputName"\Updates" | out-null;


#Step 2: Copy files from compiler output
#Step 2a: run PackageForRouterDeploy.ps1 to package up a router /deploy/ directory
$packageDeployLocation="C:\ICNET-Build\RouterBuilds"
#$packageScript
$basedir = pwd
$destdir = 'C:\ICNET-Build\RouterBuilds'
if(Test-Path -path $destdir\Deploy)
{
	del $destdir\Deploy -Force -Recurse
}
md $destdir\Deploy
# IIS Stuff
md $destdir\Deploy\wwwroot
cp $basedir\EDXLSharp_Router\ICNETREST\*.aspx $destdir\Deploy\wwwroot\
cp $basedir\EDXLSharp_Router\ICNETREST\*.png $destdir\Deploy\wwwroot\
cp $basedir\EDXLSharp_Router\ICNETREST\*.cs $destdir\Deploy\wwwroot\
cp $basedir\EDXLSharp_Router\ICNETREST\*.svc $destdir\Deploy\wwwroot\
cp $basedir\EDXLSharp_Router\ICNETREST\*.Master $destdir\Deploy\wwwroot\
cp $basedir\EDXLSharp_Router\ICNETREST\web.config $destdir\Deploy\wwwroot\
md $destdir\Deploy\wwwroot\bin
cp $basedir\EDXLSharp_Router\ICNETREST\bin\Release\*.* $destdir\Deploy\wwwroot\bin
md $destdir\Deploy\wwwroot\Images
cp $basedir\EDXLSharp_Router\ICNETREST\Images\*.* $destdir\Deploy\wwwroot\Images
md $destdir\Deploy\wwwroot\Resources
cp $basedir\EDXLSharp_Router\ICNETREST\Resources\*.* $destdir\Deploy\wwwroot\Resources
md $destdir\Deploy\wwwroot\Scripts
cp $basedir\EDXLSharp_Router\ICNETREST\Scripts\*.* $destdir\Deploy\wwwroot\Scripts
md $destdir\Deploy\wwwroot\Styles
Copy-Item $basedir\EDXLSharp_Router\ICNETREST\Styles\* $destdir\Deploy\wwwroot\Styles -Recurse
md $destdir\Deploy\wwwroot\XSLT
cp $basedir\EDXLSharp_Router\ICNETREST\XSLT\*.* $destdir\Deploy\wwwroot\XSLT
# Services
md $destdir\Deploy\wwwroot\DEProcessingService\Bin
cp $basedir\EDXLSharp_Router\DEProcessingSvc\bin\Release\*.* $destdir\Deploy\wwwroot\DEProcessingService\Bin
md $destdir\Deploy\wwwroot\ExceptionService\Bin
cp $basedir\EDXLSharp_Router\ExceptionSvc\bin\Release\*.* $destdir\Deploy\wwwroot\ExceptionService\Bin
md $destdir\Deploy\wwwroot\KeywordRoutingService\Bin
cp $basedir\EDXLSharp_Router\KeywordRoutingSvc\bin\Release\*.* $destdir\Deploy\wwwroot\KeywordRoutingService\Bin
md $destdir\Deploy\wwwroot\FederationService\Bin
cp $basedir\EDXLSharp_Router\FederationSvc\bin\Release\*.* $destdir\Deploy\wwwroot\FederationService\Bin
md $destdir\Deploy\wwwroot\DeadLetterService\Bin
cp $basedir\EDXLSharp_Router\DeadLetterSvc\bin\Release\*.* $destdir\Deploy\wwwroot\DeadLetterService\Bin
# Tools
md $destdir\Deploy\HttpLoader
cp $basedir\EDXLSharp_Tools\HttpLoader\bin\Release\*.* $destdir\Deploy\HttpLoader
md $destdir\Deploy\RouterTool
cp $basedir\EDXLSharp_Tools\RouterTool\bin\Release\*.* $destdir\Deploy\RouterTool
md $destdir\Deploy\ICNETCoreRouter
cp $basedir\EDXLSharp_Router\ICNETCoreRouter\bin\Release\*.* $destdir\Deploy\ICNETCoreRouter
md $destdir\Deploy\Config
cp $basedir\EDXLSharp_Router\ICNETServices\Config\*.* $destdir\Deploy\Config -Force

Copy-Item $packageDeployLocation"\Deploy" $outputLocation$outputName"\Updates" -Recurse
Copy-Item $buildOutputDir"\Router_Deployment\RouterDeploy.ps1" $outputLocation$outputName"\Updates"
Copy-Item $buildOutputDir"\Router_Deployment\ICNET System Admin Guide.docx" $outputLocation$outputName

#Step 3: Zip up the directory
$outputZip=$outputLocation+$outputName+".zip";
[void][System.Reflection.Assembly]::LoadFrom((Get-Location).Path+"/Router_Deployment\Ionic.Zip.dll");
$directoryToZip = $outputLocation+$outputName          
$zipfile =  new-object Ionic.Zip.ZipFile            
$e = $zipfile.AddDirectory($directoryToZip, $outputName)            
$zipfile.Save($outputZip)            
$zipfile.Dispose()  

#Step 4: Delete the output directory, leaving only the zip behind.
#remove-item $outputLocation$outputName -recurse;

#Step 5: replace the "/Lastest/" one with the new zip
$latestOutputLocation=$outputLocation+"Latest";
if (-not(test-path -path $latestOutputLocation))
    {
		mkdir $latestOutputLocation
	}
rm  $latestOutputLocation\*
copy-item $outputZip $latestOutputLocation
echo "done, zip is ready at " $outputZip

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")